package cn.gwm.utils;

import cn.hutool.core.io.FileUtil;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressWarnings("all")
public class StringUtil {
    /**
     * 字符串倒序：有递归法（不推荐）、数组倒序拼接、冒泡对调、使用StringBuffer的reverse方法等。
     * 冒泡对调（推荐）
     *
     * @param orig
     * @return
     */
    public static String reverse(String orig) {
        char[] s = orig.toCharArray();
        int n = s.length - 1;
        int halfLength = n / 2;
        for (int i = 0; i <= halfLength; i++) {
            char temp = s[i];
            s[i] = s[n - i];
            s[n - i] = temp;
        }
        return new String(s);
    }


    /**
     * 下划线转驼峰
     *
     * @param str
     * @return
     */
    public static String underlineToHump(String str) {
        String regex = "_(.)";
        Matcher matcher = Pattern.compile(regex).matcher(str);
        while (matcher.find()) {
            String target = matcher.group(1);
            str = str.replaceAll("_" + target, target.toUpperCase());
        }
        return str;
    }

    /**
     * 驼峰转下划线
     *
     * @param str 目标字符串
     * @return: java.lang.String
     */
    public static String humpToUnderline(String str) {
        String regex = "([A-Z])";
        Matcher matcher = Pattern.compile(regex).matcher(str);
        while (matcher.find()) {
            String target = matcher.group();
            str = str.replaceAll(target, "_" + target.toLowerCase());
        }
        return str;
    }

    /**
     * 检查是否为规范的驼峰命名字符串
     *
     * @param str
     * @return
     */
    public static boolean isCamelCase(String str) {
        if (!Character.isLowerCase(str.charAt(0))) {
            return false;
        }

        if (str.contains("_") || str.contains("-")) {
            return false;
        }
        // 连续字符不能为大写
        for (int i = 0; i < str.length() - 1; i++) {
            if (Character.isUpperCase(str.charAt(i)) && Character.isUpperCase(str.charAt(i + 1))) {
                return false;
            }
        }
        return true;
    }

}

